package net.loyintean.pearlshell.wechat.messagepasser.impl;

import net.loyintean.pearlshell.wechat.messagepasser.AbstractMessageBuilder;
import net.loyintean.pearlshell.wechat.model.BaseMessageModel;
import net.loyintean.pearlshell.wechat.model.common.TextMessageModel;

import org.dom4j.Document;
import org.dom4j.Element;

public class TextMessageBuilderImpl extends AbstractMessageBuilder {

	private TextMessageModel message = new TextMessageModel();

	/**
	 * @return the message
	 */
	@Override
	public BaseMessageModel getMessage(Document document) {
		document.accept(this);

		return message;
	}

	@Override
	public void visit(Element element) {
		if ("TOUSERNAME".equals(element.getName().toUpperCase())) {
			message.setToUserName(element.getText());
		}

		if ("FROMUSERNAME".equals(element.getName().toUpperCase())) {
			message.setFromUserName(element.getText());
		}

		if ("CREATETIME".equals(element.getName().toUpperCase())) {
			message.setCreateTime(Integer.parseInt(element.getText()));
		}

		if ("CONTENT".equals(element.getName().toUpperCase())) {
			message.setContent(element.getText().trim());
		}

		if ("MSGID".equals(element.getName().toUpperCase())) {
			message.setMsgId(element.getText());
		}
	}
}
